package br.org.demolay;

import java.util.List;

import br.org.demolay.webservice.Contato;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;

public class ContatosActivity extends Activity {

	ListView contatosListView;
	Context context;
	List<Contato> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contatos);

		long userId = ((GlobalState) getApplication()).getUserId();
		contatosListView = (ListView) findViewById(R.id.listContatos);
		context = this;

		LoadContatoTask task = new LoadContatoTask();
		task.execute(userId);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.contatos, menu);
		return true;
	}

	@Override
	public void onCreateContextMenu(android.view.ContextMenu menu,
			android.view.View v,
			android.view.ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Selecione");
		menu.add(0, v.getId(), 0, "Ligar para Telefone");
		menu.add(0, v.getId(), 0, "Ligar para Celular");
		menu.add(0, v.getId(), 0, "Enviar SMS");
		menu.add(0, v.getId(), 0, "Enviar Email");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		Contato contato;
		try {
			contato = list.get(info.position);

			if (item.getTitle() == "Ligar para Telefone") {
				Intent callIntent = new Intent(Intent.ACTION_DIAL);
				callIntent.setData(Uri.parse("tel:" + contato.getTelefone().replace("-", "").replace("(", "").replace(")", "")));
				startActivity(callIntent);
			} else if (item.getTitle() == "Ligar para Celular") {
				Intent callIntent = new Intent(Intent.ACTION_DIAL);
				callIntent.setData(Uri.parse("tel:" + contato.getCelular().replace("-", "").replace("(", "").replace(")", "")));
				startActivity(callIntent);
			} else if (item.getTitle() == "Enviar SMS") {
				Uri uri = Uri.parse("smsto:" + contato.getCelular().replace("-", "").replace("(", "").replace(")", ""));   
				Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
				it.putExtra("sms_body", "Demolay");   
				startActivity(it);  
			} else if (item.getTitle() == "Enviar Email") {
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("message/rfc822");
				intent.putExtra(Intent.EXTRA_SUBJECT, "Demolay");
				intent.putExtra(Intent.EXTRA_EMAIL,
						new String[] { contato.getEmail() });
				Intent mailer = Intent.createChooser(intent, null);
				startActivity(mailer);
			} else {
				return false;
			}
			return true;
		} catch (Exception e) {
			return true;
		}
	}

	public class LoadContatoTask extends AsyncTask<Long, Long, List<Contato>> {
		@Override
		protected List<Contato> doInBackground(Long... params) {
			br.org.demolay.webservice.Service service = new br.org.demolay.webservice.Service();
			return service.Contatos(params[0]);
		}

		@Override
		protected void onPostExecute(final List<Contato> result) {
			ContatoAdapter adapter = new ContatoAdapter(context, result);
			contatosListView.setAdapter(adapter);
			list = result;
			registerForContextMenu(contatosListView);
		}

		@Override
		protected void onCancelled() {
			contatosListView.setAdapter(null);
		}
	}

}
